package com.example.minesweepers;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private static final String MINESWEEPER_LARGE = "MinesweeperLarge";
	private static final String MINESWEEPER_MEDIUM = "MinesweeperMedium";
	private static final String MINESWEEPER_SMALL = "MinesweeperSmall";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


		addButton(R.id.newGameButton);
		addButton(R.id.highScoresButton);
		addButton(R.id.quitGameButton);

	}

	private void addButton(int id) {
		Button newGameButton = (Button) findViewById(id);
		newGameButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.highScoresButton:
			choseHighscoreType();
			break;
		case R.id.newGameButton:
			startActivity(OptionsActivity.class, null);
			break;
		case R.id.quitGameButton:
			exitGame();
			break;
		default:
			break;
		}
	}

	private void choseHighscoreType() {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.high_scores_dialog);
		dialog.setTitle("Chose highscore type!");

		addHighScoresDialogButton(dialog, R.id.minesweeperSmall, MINESWEEPER_SMALL);
		addHighScoresDialogButton(dialog, R.id.minesweeperMedium, MINESWEEPER_MEDIUM);
		addHighScoresDialogButton(dialog, R.id.minesweeperLarge, MINESWEEPER_LARGE);

		dialog.show();
	}

	private void addHighScoresDialogButton(Dialog dialog, int id, final String SP_FILE_NAME) {
		Button newGameButton = (Button) dialog.findViewById(id);
		newGameButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(HighScoresActivity.class, SP_FILE_NAME);
			}
		});
		
	}

	private void startActivity(Class<?> toActivity, String gameTypeStringExtra) {
		Intent optionsIntent = new Intent(MainActivity.this, toActivity);
		if (gameTypeStringExtra != null)
			optionsIntent.putExtra("GameType", gameTypeStringExtra);
		startActivity(optionsIntent);
	}

	private void exitGame() {
		onDestroy();
	}

	@Override
	protected void onDestroy() {
		// save options and/or game
		finish();
		super.onDestroy();
	}
}
